package com.lock.method;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @PackageName:com.lock.method
 * @ClassName:Test06
 * @Description:  boolean isLocked()  查询当前锁是否被线程持有
 * @Author: zhangxiaoyu
 * @date 2021/3/8 13:54
 */
public class Test06 {

    static ReentrantLock lock = new ReentrantLock();

    static  void  sm(){
        try {
            System.out.println("before lock()---"+lock.isLocked());
            lock.lock();
            System.out.println("after lock() =="+lock.isLocked());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (lock.isHeldByCurrentThread()){
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
         System.out.println("11--"+lock.isLocked());

         new Thread(new Runnable() {
             @Override
             public void run() {
                 sm();
             }
         }).start();
         
         Thread.sleep(3000);   //确保子线程执行结束
        System.out.println("22---"+lock.isLocked());
    }
}
